var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',

    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');

    },
     programclasslistget:function(){
        var _this=this;
        var paramsObj = {
            passport: _this.params.passport,
            listRows:10000,
            page:1
        };
        Api.programclasslist(paramsObj, function(data){
            console.log(data);
            _this.programclasslist(data.list)
        })
    },
    programclasslist:function(data){
        var _this=this;
        if (data && data.length > 0) {
            var html = '<option value="" selected>请选择分类</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].programClassId+'">'+data[i].className+'</option>';
            }
            $('#classifySelect').html(html);
            _this.programclasslistEvent();

        }
    },
    programclasslistEvent:function(){
        var _this=this;
        // $('#classifySelect').on('change', function(event) {
        //     event.preventDefault();
        //     _this.params.className=$(this).val();
        // });
    },
    submit:function(){
        var _this = this;
        $('#submit').click(function(event) {
            var className=[];
            var classId=[];
            if ($('.classifyBtn').length>0) {
                for (var i = 0; i < $('.classifyBtn').length; i++) {
                    className.push($('.classifyBtn').eq(i).attr('className'));
                    classId.push($('.classifyBtn').eq(i).attr('classId'));
                };
            };
            className=className.join(',');
            classId=classId.join(',');
            var title=$('#title').val();
            var fitAddress=$('#fitAddress').val();
            var crowd=$('#crowd').val();
            var email=$('#email').val();

            var keyword=$('#keyword').val();
            if ($('#is_show').hasClass('switch-on')) {
                var isRecommend=1;
            }else{
                var isRecommend=0;
            };

            var name=$('#name').val();
            var mobile=$('#mobile').val();
            var content=ue.getContent();
            var paramsObj = {
                passport: _this.params.passport,

                title:title,
                fitAddress:fitAddress,
                crowd:crowd,
                amount:1,
                className:className,
                classId:classId,
                keyword:keyword,
                name:name,
                mobile:mobile,
                email:email,
                isRecommend:isRecommend,
                content:content,
            };
            console.log(paramsObj);
            if ($('.classifyBtn').length==0) {
                layui.use('layer', function(){
                    layer.msg('请添加行业分类', {icon: 5});
                });
            }else{
                Api.projectadd(paramsObj, function(data){
                    console.log(data);
                    layui.use('layer', function(){
                        layer.msg('添加成功', {icon: 6});
                        setTimeout(function(){
                            window.location.href = Pub.getHtmlUrl('./project-list.html');
                        },1500)
                    });
                });
            };

        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#addClass').click(function(event) {
            var isCan=1;
            if ($('#classifySelect').val()!='' && $('#classifySelect').find("option:selected").text()!='') {
                $('.classifyBtn').each(function() {
                    if ($(this).attr('classId')==$('#classifySelect').val()) {
                        isCan=0;
                        return;
                    };
                });
                var html='';
                if (isCan==1) {
                    html='<a class="btn radius c-666 mr-5 classifyBtn" classId="'+$('#classifySelect').val()+'" className="'+$('#classifySelect').find("option:selected").text()+'">'+$('#classifySelect').find("option:selected").text()+'<i class="Hui-iconfont ml-5">&#xe6a6;</i></a>';
                    $('#clbefore').before(html)
                    $('#_none').css('display', 'none');
                    _this.removeClassify();
                }else{
                    layui.use('layer', function(){
                        layer.msg('该行业已添加', {icon: 5});
                    });
                };


            };
        });
    },
    removeClassify:function(){
        var _this = this;
        $('.classifyBtn').click(function(event) {
            $(this).remove();
            if ($('.classifyBtn').length==0) {
                $('#_none').css('display', 'inline-block');
            };
        });
    },
    initEvent: function(){
        var _this = this;
        _this.programclasslistget();
        _this.otherEvent();
        _this.submit();



    }
};
var page = new Page();